import config
import pymysql
import redis



conn = pymysql.connect(**config.mysql_option)
cursor = conn.cursor()
r = redis.StrictRedis(**config.redis)

def filtration(data):
    lis = []
    for i in range(len(data)):
        if data[i]!="":
            lis.append(data[i])
        else:
            pass
    return lis

def store_in_mysql(params):
    if len(params) == 10:
        print(params)
        cursor.execute("select count(*) as count from qp.test1 where third_id = %s",params[1])
        result = cursor.fetchall()
        if result[0]['count'] == 1:
            cursor.execute("update qp.test1 set plat_id = %s,account_name = %s,plat_name = %s,title_name = %s,flow_count=%s WHERE third_id = %s",
                                                    [params[0],params[3],params[4], params[6],params[7],params[1]])
        else:
            cursor.execute(
                "replace into qp.test1(plat_id ,third_id ,account_id ,account_name ,plat_name ,user_id ,title_name ,flow_count, add_time ,url) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
                params
            )
        conn.commit()
    else:
        print(params)
        print("arguments need be clean")

def plat_info_toutiao(info='med_flow_toutiao'):
        infos = r.get(info)
        infos = infos.decode("gbk",'ignore')
        len1 = len(infos)
        lis = infos.split(";")
        for i in  lis:
            try:
                params = filtration(i.split("|||")[1:-3])
                store_in_mysql(params)
            except Exception as e:
                print("e===>",e)
                pass
        now_s = r.get(info)
        if len(now_s) > len1:
            r.set(info,now_s[len1:])
        else:
            r.set(info,"")

def plat_into(info):
    infos = str(r.get(info).decode('utf-8'))
    len1 = len(infos)
    print("==========================>:",info)
    if len1>0:
        # print(infos[:1000])
        lis = infos.split(";")[:10]
        for i in  lis:
            try:
                params = filtration(i.split("|||")[1:-3])
                store_in_mysql(params)
            except Exception as e:
                print("e===>",e)
                pass
        # now_s = str(r.get(info))
        # if len(now_s) > len1:
        #     r.set(info,now_s[len1:])
        # else:
        #     r.set(info,"")
    else:
        pass        

# plat_infos = ['med_flow_aiqiyi']
plat_infos = ['med_flow_toutiao']
for i in plat_infos:
    if i == 'med_flow_toutiao':
        plat_info_toutiao()
    else:
        plat_into(i)


r.client_kill()
cursor.close()
conn.close()
r.close()



#sucess====> med_flow_toutiao,med_flow_bilibili,med_flow_miaopai,med_flow_qq,med_flow_yidian,med_flow_renren,med_flow_duanzi